package com.whfc.wxmp.api.mach.v1.service.impl;

import com.whfc.common.result.PageData;
import com.whfc.emp.dto.AppWarnEmpRecordNumDTO;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fuum.service.SysMachTypeService;
import com.whfc.mach.dto.warn.*;
import com.whfc.mach.enums.AppMachWarnRuleType;
import com.whfc.mach.param.warn.AppWarnHandleParam;
import com.whfc.mach.service.AppMachDataService;
import com.whfc.mach.service.AppMachWarnService;
import com.whfc.wxmp.api.mach.v1.service.WxmpMachWarnService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author sun_guodong
 * @description
 * @date 2020-12-14
 */
@Service
public class WxmpMachWarnServiceImpl implements WxmpMachWarnService {

    @DubboReference(interfaceClass = SysMachTypeService.class, version = "1.0.0")
    private SysMachTypeService sysMachTypeService;

    @DubboReference(interfaceClass = AppMachDataService.class, version = "1.0.0")
    private AppMachDataService appMachDataService;

    @DubboReference(interfaceClass = AppMachWarnService.class, version = "1.0.0")
    private AppMachWarnService appMachWarnService;


    @Override
    public PageData<AppWarnMachRecordDTO> machWarnList(Integer deptId, Integer pageNum, Integer pageSize, Integer handleState, Date startTime, Date endTime,Integer ruleType) {
        return appMachWarnService.recordList(pageNum, pageSize, deptId, handleState, ruleType, startTime, endTime);
    }

    @Override
    public AppWarnDetailDTO getMachWarnDetail(Integer warnId) {
        return appMachWarnService.recordDetail(warnId);
    }

    @Override
    public void handleMachWarn(WxUser loginUser, AppWarnHandleParam body) {
        body.setPhone(loginUser.getPhone());
        body.setUserName(loginUser.getNickName());
        body.setUserId(loginUser.getId());
        appMachWarnService.recordHandle(body);
    }


    @Override
    public AppMachWarnCountDTOV2 warnNumStatic(Integer deptId, Date startTime, Date endTime) {
        Map<Integer, Integer> appMachWarnCountDTO = appMachWarnService.warnNumStaticV2(deptId, 0, startTime, endTime);
        Map<Integer, Integer> appMachWarnCountDTO1 = appMachWarnService.warnNumStaticV2(deptId, 1, startTime, endTime);
        AppMachWarnCountDTOV2 ret = new AppMachWarnCountDTOV2();
        AppMachWarnRuleType[] values = AppMachWarnRuleType.values();
        ret.setHandleNum(appMachWarnCountDTO1.getOrDefault(-1,0));
        ret.setUnHandleNum(appMachWarnCountDTO.getOrDefault(-1,0));
        List<AppWarnTypeCountDTO> unHandleTypes = new ArrayList<>();
        List<AppWarnTypeCountDTO> handleTypes = new ArrayList<>();
        for (AppMachWarnRuleType ruleType:values) {
            Integer value = ruleType.value();
            Integer countUnHandle = appMachWarnCountDTO.getOrDefault(value, 0);
            Integer countHandle = appMachWarnCountDTO1.getOrDefault(value, 0);
            unHandleTypes.add(AppWarnTypeCountDTO.builder().num(countUnHandle).ruleType(value).ruleName(ruleType.tittle()).build());
            handleTypes.add(AppWarnTypeCountDTO.builder().num(countHandle).ruleType(value).ruleName(ruleType.tittle()).build());
        }
        ret.setUnHandleWarnList(unHandleTypes);
        ret.setHandleWarnList(handleTypes);
        return ret;


    }
}
